package service.univ;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import service.majorunit.MajorunitService;
import service.notice.NoticeService;
import util.manager.PageManager;
import util.paramvo.SearchParamVO;
import util.returnvo.UnivSearchListReturnVO;
import vo.MajorunitVO;
import vo.UnivVO;
import config.PageConfig;
import dao.univ.UnivDAO;

public class UnivServiceImpl implements UnivService{
	
	private UnivDAO univDAO;
	private MajorunitService majorunitService;
	private NoticeService noticeService;

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public void setUnivDAO(UnivDAO univDAO) {
		this.univDAO = univDAO;
	}

	public void setMajorunitService(MajorunitService majorunitService) {
		this.majorunitService = majorunitService;
	}

	@Override
	public UnivVO getUnivInfo(int univ_no) throws Exception {
		return univDAO.getUnivInfo(univ_no);
	}

	@Override
	public UnivSearchListReturnVO getSearchedUnivByWord(String search, String page) throws Exception {

		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}
		
		int pageNo=1;
		
		if(page!=null && !page.equals("")) pageNo=Integer.parseInt(page);
		
		List<UnivVO> univlist=univDAO.getSearchedUnivByWord(new SearchParamVO(searchlist, PageConfig.POPUP_ROW_PER_PAGE, pageNo));
		
		return new UnivSearchListReturnVO(univlist, new PageManager(PageConfig.POPUP_ROW_PER_PAGE, PageConfig.POPUP_PAGE_PER_GROUP, univDAO.countSearchedUnivByWord(searchlist), pageNo));
	
	}

	@Override
	public List<UnivVO> getUnivEditList(String search) throws Exception {

		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}else return new ArrayList<UnivVO>();
		
		return univDAO.getSearchedUnivByWord(new SearchParamVO(searchlist, PageConfig.EDIT_ROW_PER_PAGE, 1));
	}

	@Override
	public List<String> autoCompleteUnivList(String search) throws Exception {

		ArrayList<String> searchlist=new ArrayList<String>();
		
		if(search!=null && !search.trim().equals("")){
			StringTokenizer tokenizer=new StringTokenizer(search);
			
			while (tokenizer.hasMoreElements()) {
				searchlist.add(tokenizer.nextToken());
			}
		}else return new ArrayList<String>();
		
		List<UnivVO> univlist=univDAO.getSearchedUnivByWord(new SearchParamVO(searchlist, PageConfig.AUTOCOMPL_ROW_PER_PAGE, 1));
		
		ArrayList<String> returnlist=new ArrayList<String>();
		
		for(UnivVO vo : univlist){
			returnlist.add(vo.getUniv_name());
		}
		
		return returnlist;
	}

	@Override
	public void registerUniv(UnivVO param) throws Exception {
		univDAO.registerUniv(param);
	}

	@Override
	public void updateUniv(UnivVO param) throws Exception {
		
		String oldName=getUnivInfo(param.getUniv_no()).getUniv_name();
		String newName=param.getUniv_name();
		
		if(!oldName.equals(newName)){
			for(MajorunitVO vo : majorunitService.getMajorunits(param.getUniv_no())){
				int majorunit_no=vo.getMajorunit_no();
				String majorunit_name=majorunitService.getMajorunitReturnVO(vo).getMajorunit_name();
				
				noticeService.toWriters(
						majorunit_no, 
						oldName+" 이름 변경", 
						"글을 작성하신 "+majorunit_name+"의<br>"+oldName+"가 "+newName+"로 변경되었습니다.");
				
				noticeService.toInterestedMember(
						majorunit_no, 
						oldName+" 이름 변경", 
						"관심학과로 등록하신 "+majorunit_name+"의<br>"+oldName+"가 "+newName+"로 변경되었습니다.");
			}
		}
		
		univDAO.updateUniv(param);
	}

	@Override
	public void deleteUniv(int univ_no) throws Exception {
		
		String univ_name=getUnivInfo(univ_no).getUniv_name();
		
		univDAO.deleteUniv(univ_no);
		
		for(MajorunitVO vo : majorunitService.getMajorunits(univ_no)){
			int majorunit_no=vo.getMajorunit_no();
			String majorunit_name=majorunitService.getMajorunitReturnVO(vo).getMajorunit_name();
			
			noticeService.toWriters(
					majorunit_no, 
					univ_name+" 폐교", 
					univ_name+"이 폐교하여 "+majorunit_name+"에 작성하신 글이 노출되지 않게 되었습니다.<br> 마이페이지에서는 열람이 가능합니다.");
			
			noticeService.toInterestedMember(
					majorunit_no, 
					univ_name+" 폐교", 
					univ_name+"이 폐교하여 관심학과로 등록하신 "+majorunit_name+"에 접근할 수 없게 되었습니다.");
		}
		
		majorunitService.deleteMajorunitInUniv(univ_no);
	}
}
